﻿var nowBgm = null;
var nowVoice = null;
//
function GameWrite(load, xml) {
    this._load = load;
    this._xml = xml;
    this.nextXml = null;
    this.node = this.GetRoot();
    this.loadIndex = 0;
    this.xmlLoading = false;
    this.textBox_context = new TextBox("text_canvas");
    this.cg_context = new CGcanvas("cg_canvas");
    this.peopleHead_context = new PeopleHeadcanvas("ph_canvas");
    this.bgm = new Bgm();
    this.voice = new Voice();
    this.xmlEnd = false;
    //Event
    this.nextEvent = null;
    this.onStartWrite = null;
    this.onWriteEnd = null;
}
GameWrite.prototype.appXml = function (xml, load) {
    this.xmlLoading = false;
    if (xml.getElementsByTagName("Load").length != 0) {
        this.nextXml = xml;
        load.LoadXmlObjectToArray(this.nextXml);
    }
    else {
        this.xmlEnd = true;
    }
}
GameWrite.prototype.GetRoot = function () {
    return this._xml.firstChild;
}
GameWrite.prototype.Next = function () {
    if (this.node == null) return true;
    //返回false表示不移动节点（循环）
    if (this.nextEvent && !this.nextEvent())
        return true;
    if (this.node.hasChildNodes()) {
        this.node = this.node.firstChild;
    }
    else {
        var nextNode = this.node.nextSibling;
        if (nextNode != null)
            this.node = nextNode;
        else
            this.node = this.GetParent(this.node);
    }
    if (this.node == null) {
        if (this.nextXml == null) {
            if (this.xmlLoading) {
                ShowMask(true);
                var _this = this;
                var _int = window.setInterval(function () {
                    if (!_this.xmlLoading && _this.nextXml != null) {
                        window.clearInterval(_int);
                        _this._xml = _this.nextXml;
                        _this.nextXml = null;
                        _this.node = _this.GetRoot();
                        _this.loadIndex = 0;
                        HideMask(true);
                    }
                }, 300);
                return true;
            }
            else
                return false; //end
        }
        else {
            this._xml = this.nextXml;
            this.nextXml = null;
            this.node = this.GetRoot();
            this.loadIndex = 0;
            return true;
        }
    }
    else return true;
}
GameWrite.prototype.GetParent = function (node) {
    if (node.parentNode.nodeName == "GalGame") return null;
    if (node.parentNode.nextSibling != null)
        return node.parentNode.nextSibling;
    else
        return this.GetParent(node.parentNode);
}
GameWrite.prototype.Preload = function () {
    if (arguments.length != 0)
        this._load.LoadXmlObjectToArray(arguments[0]);
    else if (this.loadIndex > config.load / 2 && this.nextXml == null && !this.xmlLoading && !this.xmlEnd) {
        this.xmlLoading = true;
        this._load.ReadXml({
            callback: this.appXml,
            errcallback: this.error,
            context: this
        });
    }
    this._load.Preload(Show);
}
GameWrite.prototype.Write = (function () {
    var setEvent = function (_this) {
        function lock() {
            if (!_this.cg_context.cg_show_state &&
                !_this.textBox_context.textBox_state &&
                !_this.peopleHead_context.peopleHead_show_state &&
                !_this.textBox_context.text_print_state) {
                return true;
            }
            else
                return false;
        }
        _this.nextEvent = lock;
        _this.onStartWrite = lock;
    }
    var setParameter = function (obj, parameters, node) {
        if (node.nodeName != "#text") {
            var fun = null;
            if (node.getAttribute("Fun") != null && node.getAttribute("Fun") != "") {
                fun = node.getAttribute("Fun");
            }
            obj.NewAnimateFun(fun);
        }
        obj.SetCallBack(function () { Next(); });
        obj.SetArg(parameters);
    }
    return function () {
        if (this.node == null) return false;
        if (this.onStartWrite && !this.onStartWrite())
            return false;
        //返回false不移动节点
        var node = this.node;
        if (node.nodeName == "Load") {
            if (node.parentNode.nodeName == "GalGame") this.loadIndex++;
        }
        else {
            setEvent(this);
            var arg = Tool.NodeAttributesToObj(node);
            var name = node.nodeName;
            var parentName = node.parentNode.nodeName;

            if (name == "CG" && parentName == "Load") {
                var parameters = this.CG_setParameters(arg);
                setParameter(this.cg_context, parameters, node);
                if (!this.CG_write(node, parameters)) return false;
            }
            else if (name == "Bgm" && parentName == "Load") {
                var parameters = this.Bgm_setParameters(arg);
                if (!this.Bgm_write(node, parameters)) return false;
            }
            else if (name == "TextList" && parentName == "Load") {

            }
            else if (name == "TextBox" && parentName == "TextList") {
                var parameters = this.TextBox_setParameters(arg);
                setParameter(this.textBox_context, parameters, node);
                if (!this.TextBox_write(node, parameters)) return false;
            }
            else if (name == "People" && parentName == "TextBox") {
                //if (!this.WritePeople(node)) return false;
            }
            else if (name == "Ph") {
                var parameters = this.PeopleHead_setParameters(arg);
                setParameter(this.peopleHead_context, parameters, node);
                if (!this.PeopleHead_write(node, parameters)) return false;
            }
            else if (name == "Label" && parentName == "TextBox") {
                var parameters = this.Label_setParameters(arg);
                setParameter(this.textBox_context, parameters, node);
                if (!this.Label_write(node, parameters)) return false;
            }
            else if (name == "#text" && parentName == "Label") {
                var parameters = this.TextNode_setParameters(Tool.NodeAttributesToObj(node.parentNode));
                setParameter(this.textBox_context, parameters, node);
                if (!this.TextNode_write(node, parameters)) return false;
            }
        }
        if (this.onWriteEnd && !this.onWriteEnd())
            return false;
        return true;
    }
})()

//----------------------------------------------
//          CG
//----------------------------------------------
GameWrite.prototype.CG_setParameters = function (arg) {
    return arg;
}
GameWrite.prototype.CG_write = function (node, arg) {
    if (arg) {
        if (arg.ClearCG && arg.ClearCG == "True") {
            this.cg_context.ClearCG();
            return false;
        }
        else if (arg.Src) {
            var cg = this._load.cgArray.pop();
            this._load.cgLoaded--;
            SystemInfo(this._load);
            logManager.remove(cg.id);
            this.cg_context.DrawCG(cg);
            return false;
        }
    }
    return true;
}
//----------------------------------------------
//          BGM
//----------------------------------------------
GameWrite.prototype.Bgm_setParameters = function (arg) {
    return arg;
}
GameWrite.prototype.Bgm_write = function (node, arg) {
    if (arg) {
        if (arg.Stop && arg.Stop == "True") {
            this.bgm.stop(arg);
            return true;
        }
        else if (arg.Src) {
            var bgm = this._load.bgmArray.pop();
            this._load.bgmLoaded--;
            this.bgm.play(bgm, arg);
            SystemInfo(this._load);
            logManager.remove(bgm.id);
        }
    }
    return true;
}
//----------------------------------------------
//          TextBox
//----------------------------------------------
GameWrite.prototype.TextBox_setParameters = function (arg) {
    return arg;
}
GameWrite.prototype.TextBox_write = function (node, arg) {
    if (arg) {
        if (arg.Hide && arg.Hide == "True") {
            this.textBox_context.boxHide();
            return false;
        }
        else if (arg.Show && arg.Show == "True") {
            this.textBox_context.boxShow();
            return false;
        }
    }
    return true;
}
//----------------------------------------------
//          People
//----------------------------------------------
GameWrite.prototype.WritePeople = function (node) {
    var ImageShowEnd = function () {
        _this.onStartWrite = function () { return true; }
        Show();
    }
    if (node.getAttribute("Src") != null && node.getAttribute("Src") != "") {
        var people = this._load.peopleArray.pop();
        this._load.peopleLoaded--;
    }
    return true;
}
//----------------------------------------------
//          PeopleHead
//----------------------------------------------
GameWrite.prototype.PeopleHead_setParameters = function (arg) {
    return arg;
}
GameWrite.prototype.PeopleHead_write = function (node, arg) {
    if (arg) {
        if (arg.Clear && arg.Clear == "True") {
            this.peopleHead_context.ClearPeopleHead();
            return false;
        }
        else if (arg.Src != null && arg.Src != "") {
            var ph = this._load.peopleHeadArray.pop();
            this._load.peopleHeadLoaded--;
            SystemInfo(this._load);
            logManager.remove(ph.id);
            this.peopleHead_context.DrawPeopleHead(ph);
            return false;
        }
    }
    return true;
}
//----------------------------------------------
//          Label
//----------------------------------------------
GameWrite.prototype.Label_setParameters = function (arg) {
    return arg;
}
GameWrite.prototype.Label_write = function (node, arg) {
    if (arg) {
        if (arg.Voice && arg.Voice != "") {
            var voice = this._load.voiceArray.pop();
            this._load.voiceLoaded--;
            this.voice.play(voice, arg);
            SystemInfo(this._load);
            logManager.remove(voice.id);
        }
        else {
            this.voice.stop(arg);
        }
        if (arg.Model != null && arg.Model != "") {
            switch (arg.Model) {
                case "Clear":
                    this.textBox_context.Clear();
                    return false;
            }
        }
    }
    return true;
}
//----------------------------------------------
//          TextNode
//----------------------------------------------
GameWrite.prototype.TextNode_setParameters = function (arg) {
    return arg;
}
GameWrite.prototype.TextNode_write = function (node, arg) {
    this.onStartWrite = function () {
        if (this.textBox_context.text_print_state) {
            this.textBox_context.Stop();
            return false;
        }
        else
            return true;
    }
    this.textBox_context.DrawText(node.nodeValue);
    return false;
}
GameWrite.prototype.error = function (XMLHttpRequest, textStatus, errorThrown) {
    alert("脚本数据请求失败");
    this.Preload();
}